var gridCount = 0;

$(function() 
{
	if(!isEmptyStr($("#isSuccess").val()))
	{
		alert($("#isSuccess").val());
	}
});

/**
 * 画面初期化处理
 */
$(function() {	
	setListTableCss("dataInfo");
});

/**
 * 下拉框改变.
 * @return
 */
function changeJob()
{
	$('#itemId').val('');
	doAction(document.forms["jobEvaEntryForm"], "jobeva/jobEvaEntry-add.action");
}

/**
 * 下拉框改变.
 * @return
 */
function changeItem()
{
	doAction(document.forms["jobEvaEntryForm"], "jobeva/jobEvaEntry-add.action");
}

/**
 * 录入岗位考核
 * @return
 */
function save()
{
	if (doCheck())
	{
		doAction(document.forms["jobEvaEntryForm"], "jobeva/jobEvaEntry-save.action");
	}
}

/**
 * 画面输入检查
 * @return
 */
function doCheck()
{
	if (isEmptyStr($('#jobId').val()))
	{
		alert(getMessage(MESSAGE_002, '岗位'));
		return false;
	}
	
	if (isEmptyStr($('#itemId').val()))
	{
		alert(getMessage(MESSAGE_002, '考核项目'));
		return false;
	}

	if (isEmptyStr($('#evaTime').val()))
	{
		alert(getMessage(MESSAGE_002, '考核时间'));
		return false;
	}

	if (isEmptyStr($('#remark').val()))
	{
		alert(getMessage(MESSAGE_001, '备注'));
		return false;
	}
	else if ($('#remark').val().Trim().length > 1000)
	{
		alert(getMessage(MESSAGE_003, '备注', '1000'));
		return false;
	}
	return true;
}

function checkStr(o, itemName, maxLength)
{

	if (!isEmptyStr(o.value)) 
	{
		var value = o.value.Trim();

		if (value.length > maxLength) 
		{
			alert(getMessage(MESSAGE_003, itemName, maxLength));
			o.select();
			return false;
		}
	}
}

/**
 * 新增行
 * @return
 */
function addRow()
{
	var rows = getRowTemplate(gridCount);
    $(rows).insertAfter("#rule tr:last");
    gridCount = gridCount + 1;
}

function getRowTemplate(index)
{
	return  "<tr id=\"row_" + index + "\" >" +
			"<td><input type='text' name='jobItemEntity.jobRuleList[" + index + "].ruleName' style='width: 99%' onblur=\"checkStr(this, '规则名称', 200);\"/></td>" +
			"<td><input type='text' name='jobItemEntity.jobRuleList[" + index + "].workerName' id='workerName_" + index + "' style='width: 80%'/>" +
				"<input type='hidden' name='jobItemEntity.jobRuleList[" + index + "].workerCode' id='workerCode_" + index + "'/>" +
				"<a href='javascript: void(0);' onclick=\"selectWorker('" + index + "'); return false;\">&nbsp;&nbsp;<img src='images/f.gif' style='vertical-align: middle' border='0'>选择&nbsp;&nbsp;</a></td>" +
			"<td><input type='text' name='jobItemEntity.jobRuleList[" + index + "].score' class='input numberField' style='width: 80%;' onkeydown=\"checkDecimal(this.value, event)\" maxlength=\"10\"/><label id='label_" + index + "' style='display: none;'>%</label></td>" +
			"<td><input type='radio' name='jobItemEntity.jobRuleList[" + index + "].scoringWay' id='jobItemEntity.jobRuleList[" + index + "].scoringWay1' checked='checked' value='1' onclick='scoringWayChange(this, " + index + ")'/>" +
				"<label for='jobItemEntity.jobRuleList[" + index + "].scoringWay1'>数值</label>" +
				"&nbsp;<input type='radio' name='jobItemEntity.jobRuleList[" + index + "].scoringWay' id='jobItemEntity.jobRuleList[" + index + "].scoringWay2' value='2' onclick='scoringWayChange(this, " + index + ")'/>" +
				"<label for='jobItemEntity.jobRuleList[" + index + "].scoringWay2'>百分比</label></td>" +
			"<td><input type='text' name='jobItemEntity.jobRuleList[" + index + "].remark' style='width: 99%' onblur=\"checkStr(this, '备注', 1000);\"/></td>" +
			"<td nowrap='nowrap' align='center'><a href=javascript: void(0); onclick=deleteJobItem('row_" + index + "'); return false;><img src=images/delete.jpg style=vertical-align: middle border=0>删除&nbsp;&nbsp;</a></td>";
	
}
/**
 * 删除行
 * @param id
 * @return
 */
function deleteJobItem(id)
{
	if(confirm("确定要删除此条记录吗？"))
	{
		$("#rule tr").each(function (key, value) {
			if (this.id == id)
			{
				$(this).remove();
				return;
			}
		});
	}
}

/**
 * 计分方式改变
 * 
 * @param o
 * @param index
 * @return
 */
function scoringWayChange(o, index)
{
	if (o.value == '1')
	{
		$('#label_' + index).hide();
	}
	else
	{
		$('#label_' + index).show();
	}
}

/**
 * 选择工作人员
 * 
 * @param index
 * @return
 */
function selectWorker(index)
{
	var value = window.showModalDialog(path + '/system/worker-selectList.action', null, 'dialogHeight=500px; dialogWidth=700px;');
	if (value)
	{
		$('#workerCode_' + index).val(value.workerCode);
		$('#workerName_' + index).val(value.workerName);
	}
}
